package com.hotelManager.www.service;

import com.hotelManager.www.dao.RoomDao;
import com.hotelManager.www.dao.RoomTypeDao;
import com.hotelManager.www.po.Room;
import com.hotelManager.www.po.RoomType;
import com.hotelManager.www.util.JDBCUtils;
import java.sql.Connection;
import java.sql.SQLException;
import java.math.BigDecimal;
import java.util.List;

public class RoomService implements RoomServiceInterface {
    private final RoomDao roomDao = new RoomDao();
    private final RoomTypeDao roomTypeDao = new RoomTypeDao();

    @Override
    public Connection getConnection() throws SQLException {
        try {
            return JDBCUtils.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean create(Room room) {
        try (Connection conn = getConnection()) {
            return roomDao.create(conn, room);
        } catch (SQLException e) {
            throw new RuntimeException("创建房间失败", e);
        }
    }

    @Override
    public boolean update(Room room) {
        try (Connection conn = getConnection()) {
            return roomDao.update(conn, room);
        } catch (SQLException e) {
            throw new RuntimeException("更新房间失败", e);
        }
    }

    @Override
    public boolean delete(Integer id) {
        try (Connection conn = getConnection()) {
            return roomDao.delete(id);
        } catch (SQLException e) {
            throw new RuntimeException("删除房间失败", e);
        }
    }

    @Override
    public Room findById(Integer id) {
        try (Connection conn = getConnection()) {
            return roomDao.findById(id);
        } catch (SQLException e) {
            throw new RuntimeException("获取房间失败", e);
        }
    }

    @Override
    public List<Room> findAll() {
        try (Connection conn = getConnection()) {
            return roomDao.findAll();
        } catch (SQLException e) {
            throw new RuntimeException("获取所有房间失败", e);
        }
    }

    @Override
    public List<Room> getRoomsByShopId(int shopId) {
        try (Connection conn = getConnection()) {
            return roomDao.findByShopId(conn, shopId);
        } catch (SQLException e) {
            throw new RuntimeException("获取店铺房间失败", e);
        }
    }

    @Override
    public boolean updateRoomPrice(int roomId, BigDecimal newPrice) {
        try (Connection conn = getConnection()) {
            Room room = roomDao.findById(roomId);
            if (room != null) {
                return roomDao.update(conn, room);
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException("更新房间价格失败", e);
        }
    }

    @Override
    public boolean updateRoomStock(int roomId, int newStock) {
        try (Connection conn = getConnection()) {
            Room room = roomDao.findById(roomId);
            if (room != null) {
                return roomDao.update(conn, room);
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException("更新房间库存失败", e);
        }
    }

    @Override
    public boolean isRoomAvailable(int roomId) {
        Room room = findById(roomId);
        if (room == null) {
            return false;
        }
        try (Connection conn = getConnection()) {
            RoomType roomType = roomTypeDao.findById(room.getRoomTypeId());
            return roomType != null && roomType.getStock() > 0 && "available".equals(room.getStatus());
        } catch (SQLException e) {
            throw new RuntimeException("检查房间可用性失败", e);
        }
    }

    @Override
    public int getRoomStock(int roomId) {
        Room room = findById(roomId);
        if (room == null) {
            return 0;
        }
        try (Connection conn = getConnection()) {
            RoomType roomType = roomTypeDao.findById(room.getRoomTypeId());
            return roomType != null ? roomType.getStock() : 0;
        } catch (SQLException e) {
            throw new RuntimeException("获取房间库存失败", e);
        }
    }

    @Override
    public List<Room> searchRoomsByType(int shopId, String roomType) {
        try (Connection conn = getConnection()) {
            return roomDao.findByType(conn, shopId, roomType);
        } catch (SQLException e) {
            throw new RuntimeException("搜索房间类型失败", e);
        }
    }

    @Override
    public boolean isValidPrice(BigDecimal price) {
        return price != null && price.compareTo(BigDecimal.ZERO) >= 0;
    }

    @Override
    public boolean isValidStock(int stock) {
        return stock >= 0;
    }

    /**
     * 通过房间号查找房间
     */
    @Override
    public Room findByRoomCode(String roomCode) {
        try (Connection conn = getConnection()) {
            return roomDao.findByRoomCode(conn, roomCode);
        } catch (SQLException e) {
            throw new RuntimeException("通过房间号查找房间失败", e);
        }
    }

    @Override
    public List<RoomType> getRoomTypesByShopId(int shopId) {
        try (Connection conn = getConnection()) {
            return roomTypeDao.findByShopId(conn, shopId);
        } catch (SQLException e) {
            throw new RuntimeException("获取房型列表失败", e);
        }
    }

    @Override
    public List<Room> getRoomsByRoomTypeId(int roomTypeId) {
        try (Connection conn = getConnection()) {
            return roomDao.findByRoomTypeId(conn, roomTypeId);
        } catch (SQLException e) {
            throw new RuntimeException("获取房型房间列表失败", e);
        }
    }
}
